package org.chengn.level_middle.context1457;


import org.chengn.definition.TreeNode;

public class Context1457 {
    public int pseudoPalindromicPaths (TreeNode root) {
        return dfs(root, (long) 2 << root.val);
    }

    int dfs(TreeNode root, long val) {
        if (root.left == null && root.right == null) {
            return checkVal(val) ? 1 : 0;
        }
        int l = root.left != null ? dfs(root.left, val ^ (2L << root.left.val)) : 0;
        int r = root.right != null ? dfs(root.right, val ^ (2L << root.right.val)) : 0;
        return l + r;
    }

    boolean checkVal(long val) {
        int idx = 0;
        for (int i = 0; i < 10; ++i) {
            if ((val & 2 << i) != 0) idx++;
        }
        return idx <= 1;
    }

}
